package com.ruoyi.system.mq;


import com.ruoyi.system.domain.SysHouseholdTask;
import com.ruoyi.system.mapper.SysHouseholdTaskMapper;
import io.netty.util.CharsetUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@RocketMQMessageListener(
        consumerGroup = "consumer-household-group", // 组名
        topic = "test-topic", // 和生产者一样   监听消息
        selectorExpression = "test-tag", // 标题
        messageModel = MessageModel.CLUSTERING // 集群        还有广播   不常用
)
public class Consumer implements RocketMQListener<MessageExt> {

    @Autowired
    private SysHouseholdTaskMapper sysHouseholdTaskMapper;

    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody(); // 将数组转为string对象
        String message = new String(body, CharsetUtil.UTF_8);
        System.out.println(message);
        if (!StringUtils.isEmpty(message)){
            Integer i = 0;
            List<SysHouseholdTask> list = sysHouseholdTaskMapper.selectIncompleteTaskList(i);
            for (SysHouseholdTask sht:list) {
                String name = sht.getName();
                String task = sht.getTask();
                Integer state1 = sht.getState();
                System.out.println("姓名："+ name + "  " + "任务名："+ task + "  " + "状态：" + state1);
            }
        }

    }
}
